//BeanCipher - Bean Cipher Security Layer
//Copyright (C) 2005 Raul Alfredo Bajales
//
//This library is free software; you can redistribute it and/or
//modify it under the terms of the GNU Lesser General Public
//License as published by the Free Software Foundation; either
//version 2.1 of the License, or (at your option) any later version.
//
//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//Lesser General Public License for more details.
//
//You should have received a copy of the GNU Lesser General Public
//License along with this library; if not, write to the Free Software
//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package ar.com.rab.beancipher.impl.configuration;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import ar.com.rab.beancipher.spi.IBeanDescriptor;
import ar.com.rab.beancipher.spi.IBeanMapping;

/**
 * Implementation of <code>IBeanMapping</code> interface.
 * 
 * @see ar.com.rab.beancipher.spi.IBeanMapping
 * @author Bajales Raul
 *
 */
public class BeanMapping implements IBeanMapping {

    private Set mappings;
    
    /**
     * Constructor
     */
    public BeanMapping() {
        super();
        this.mappings = new HashSet();
    }
    
    /* (non-Javadoc)
     * @see ar.com.rab.beancipher.impl.configuration.IBeanMapping#addMapping(ar.com.rab.beancipher.impl.configuration.BeanDescriptor)
     */
    public void addMapping(IBeanDescriptor descriptor) {
        this.mappings.add(descriptor);
    }
    
    /* (non-Javadoc)
     * @see ar.com.rab.beancipher.impl.configuration.IBeanMapping#getDescriptorFor(java.lang.String)
     */
    public IBeanDescriptor getDescriptorFor(String beanClassName) {
        List list = new ArrayList(this.mappings);
        Collections.sort(list);
        int pos = Collections.binarySearch(list, new BeanDescriptor(beanClassName, null));
        if (pos != -1) return (IBeanDescriptor) list.get(pos);
        return null;
    }

}
